package algorithm_optimal_solution.StackAndQueue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.LinkedList;

//最大值减去最小值小于或等于num的子数组数量：创建两个与生成窗口最大数组使用类似的双端队列。
//a[i..j]两个下标，j从左到右每次移动一位，当j不满足条件的时候移动i，然后再移动j。
//每次满足条件max(arr[i...j] - min(arr[i...j]) <= num的数量为j - i
public class SubarrayNum {

    public void getNum(int[] arr,int num){
        Deque<Integer> minList = new LinkedList<>();
        Deque<Integer> maxList = new LinkedList<>();
        int i = 0;
        int j = 0;
        int count = 0;
        while(i < arr.length){
            while(j < arr.length){
                if(minList.isEmpty() || minList.peekLast() != j){
                    while(!minList.isEmpty() && arr[minList.peekLast()] >= arr[j]){
                        minList.pollLast();
                    }
                    minList.offerLast(j);
                    while(!maxList.isEmpty() && arr[maxList.peekLast()] <= arr[j]){
                        maxList.pollLast();
                    }
                    maxList.offerLast(j);
                }
                if(arr[maxList.getFirst()] - arr[minList.getFirst()] > num){
                    break;
                }
                j++;
            }
            count += j - i;
            if(minList.peekFirst() == i){
                minList.pollFirst();
            }
            if(maxList.peekFirst() == i){
                maxList.pollFirst();
            }
            i++;
        }
        System.out.println(count);
    }

    public static void main(String[] args)throws IOException{
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        String[] value = scanner.readLine().split(" ");
        int len = Integer.parseInt(value[0]);
        int num = Integer.parseInt(value[1]);
        String[] numbers = scanner.readLine().split(" ");
        int[] arr = new int[len];
        for(int i = 0;i<len;i++){
            arr[i] = Integer.parseInt(numbers[i]);
        }
        new SubarrayNum().getNum(arr,num);
    }
}
